<template>
  <div>
    <div style="margin: 10px 0">
      <el-button @click="add()" type="primary"><i style="margin-right: 5px" class="el-icon-circle-plus-outline"></i>新增</el-button>
      <el-button @click="batchDel" type="danger"><i style="margin-right: 5px" class="el-icon-remove-outline"></i>批量删除</el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"  row-key="id" default-expand-all @selection-change="handleSelectionChange">
      <el-table-column type="selection" ></el-table-column>
      <el-table-column prop="id" label="ID" width="80px"></el-table-column>
      <el-table-column prop="mname" label="名称" ></el-table-column>
      <el-table-column prop="path" label="路径" ></el-table-column>
      <el-table-column prop="pagePath" label="页面路径" ></el-table-column>
      <el-table-column prop="description" label="描述" ></el-table-column>
      <el-table-column label="操作" width="300px">
        <template slot-scope="scope">
          <el-button type="success" @click="Edit(scope.row)"><i class="el-icon-edit" style="margin-right: 5px;width:10px"></i>编辑</el-button>
          <el-button type="primary" @click="add(scope.row.id)" v-if="!scope.row.pid && !scope.row.path"><i class="el-icon-plus" style="margin-right: 5px;width: 20px"></i>新增子菜单</el-button>
          <el-button type="danger" @click="Delete(scope.row.id)"><i class="el-icon-delete-solid" style="margin-right: 5px;width: 10px"></i>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog title="菜单新增" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small" :model="form" :rules="rules" ref="form">
        <el-form-item label="名称" prop="mname">
          <el-input v-model="form.mname" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="path">
          <el-input v-model="form.path" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="页面路径" prop="pagePath">
          <el-input v-model="form.pagePath" autocomplete="off" ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input  v-model="form.description" autocomplete="off" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="菜单修改" :visible.sync="uDialogFormVisible" width="30%">
    <el-form label-width="80px" size="small" :model="form" :rules="rules" ref="form">
      <el-form-item label="名称" prop="mname">
        <el-input v-model="form.mname" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item label="路径" prop="path">
        <el-input v-model="form.path" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item label="页面路径" prop="pagePath">
        <el-input v-model="form.pagePath" autocomplete="off" ></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input  v-model="form.description" autocomplete="off" ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="uDialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('form')">确 定</el-button>
    </div>
    </el-dialog>



  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      mname: '',
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      uDialogFormVisible:false,
      headerBg: 'headerBg',
      form: {
        id: '',
        mname: '',
        path:'',
        pagePath: '',
        pid: '',
        description: '',
      },
      rules: {
        name: [{required: true, message: '请输入角色名称', trigger: 'blur'}],
        path: [{required: false, message: '请输入路 径'}],
        description: [{required: false, message: '请输入描  述'}],
      },
    }
  },
  created() {
    // 请求分页查询数据
    this.getPageData()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.getPageData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getPageData()
    },
    reset() {
      this.mname = "";
      this.getPageData()
    },
    add(id) {
      this.dialogFormVisible = true;
      this.form = {};
      if(id){
        this.form.pid = id;
      }
    },
    Edit(row) {
      this.form.id = row.id;
      this.form.mname = row.mname;
      this.form.path = row.path;
      this.form.pagePath = row.pagePath;
      this.form.description = row.description;
      this.uDialogFormVisible = true;
    },
    Delete(id) {
      this.$confirm('确定要删除该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/menu/deleteById/" + id).then(res => {
          if (res.code === 1000) {
            this.$message({
              type: 'info',
              message: res.data
            });
            this.getPageData()
          } else {
            this.$message({
              type: 'info',
              message: res.data
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.request.post("/menu/save", this.form)
              .then(res => {
                if (res.code === 1000) {
                  this.$message({
                    showClose: true,
                    message: res.data,
                    type: 'success'
                  });
                  this.dialogFormVisible = false;
                  this.uDialogFormVisible = false;
                  this.getPageData()
                } else {
                  this.$message({
                    showClose: true,
                    message: res.data,
                    type: 'error'
                  });
                  this.resetForm(formName)
                }
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 批量删除
    handleSelectionChange(val) {
      this.multipleSelection = [];
      this.multipleSelection = val;
    },
    batchDel() {
      // 下面这一句是将multipleSelection中的每一行的对象转换成纯id的数组
      let ids = this.multipleSelection.map(val => val.id)


      this.$confirm('确定要删除这些用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.post("/menu//batchDelete", ids)
            .then(res => {
              if (res.code === 1000) {
                this.$message({
                  showClose: true,
                  message: res.data,
                  type: 'success'
                });
              }
              this.getPageData()
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },


    // 获取查询数据
    getPageData() {
      this.request.get("/menu/findAll",{})
          .then(res => {
            this.tableData = []
            this.tableData = res.data

          })

    }
  }
}
</script>


<style>
.headerBg {
  background: #eee !important;
}
</style>